#region Imported Namespaces
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using BO = PHSRAG.Insight21.BusinessObjects;
using DataAccess = PHSRAG.Utility.DataAccess;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
////using ISecurityPolicy = PHSRAG.Insight21.Policy.ISecurityPolicy;
#endregion

namespace PHSRAG.Insight21.Personnel
{
	/// <summary>
	/// Salary Grids class defines the method for loading the entire set of EmployeeJobMapIds.
	/// </summary>
	public class SalaryGrids :BO.BusinessObject
	{
		#region Constants
		private const string SalaryGridsKey = "SalaryGrids_DF0F5149-F492-4941-A596-E65AEF5D4550";
		#endregion

		#region Constructor
		/// <summary>
		/// Constructor with cachePolicy and securityPolicy.
		/// </summary>
		/// <param name="cachePolicy"></param>
		/// <param name="securityPolicy"></param>
		public SalaryGrids(ICachePolicy cachePolicy) : base(cachePolicy)
		{
		}
		#endregion

		#region BusinessObject Methods
		/// <summary>
		/// Cache the salaryGrids object  using the caching policy.
		/// </summary>
		public override void Cache()
		{
			CachePolicy.Cache(SalaryGridsKey, this);
		}
		/// <summary>
		/// Uncache the salaryGrids object using the caching policy.
		/// </summary>
		public override void UnCache()
		{
			CachePolicy.UnCache(SalaryGridsKey);
		}
		/// <summary>
		/// Load the DataSet with EmployeeJobMapIDs ,totals of all columns,total rows affected and feed details by executing the				stored procedure "GetSalaryGrid".  
		/// Throws system exception if an error is encountered  during the load process.
		/// </summary>
		/// <param name="connectionString">connection string Info.</param>
		/// <param name="args">user define parameters.</param>
		public override void Load(string connectionString, params object[] args)
		{
			try
			{
				DataSet salaryGridData = DataAccess.GetDataSet(connectionString, "GetSalaryGrid",
					DataAccess.BuildSqlParameterArray(
					 "@employeeFirstName", SqlDbType.VarChar, args[0]
					, "@employeeLastName",SqlDbType.VarChar,args[1]
					, "@employeeNumber", SqlDbType.VarChar,args[2]
					, "@jobCode", SqlDbType.VarChar, args[3]
					, "@statusId",SqlDbType.Int,args[4]
					, "@hrDptId",SqlDbType.Int,args[5]
					, "@payTypeId",SqlDbType.Int,args[6]
					, "@salEffectiveDateFrom", SqlDbType.DateTime, Convert.ToDateTime(args[7])
					, "@salEffectiveDateTo",SqlDbType.DateTime, Convert.ToDateTime(args[8])
					, "@fiscalYearId",SqlDbType.Int,args[9]
					, "@pi",SqlDbType.Int,args[10]
					, "@labId", SqlDbType.Int,args[11]
					, "@employeeJobMapId", SqlDbType.Int, args[12]
					, "@domainUserId", SqlDbType.Int,args[13]
					));
		
				// sort on specified columns;
				DataView salaryGridDataView = new DataView(salaryGridData.Tables[0]);
				salaryGridDataView.Sort = String.Format("{0} {1}",args[14], args[15]);

				if (Convert.ToInt16(args[16]) == 0) 
				{
					// Remove all columns from the data view except the PerEmployeeJobMapId column.
					for (int columnCount = salaryGridDataView.Table.Columns.Count - 1; columnCount >= 0; --columnCount)
					{
						switch (salaryGridDataView.Table.Columns[columnCount].Caption.ToLower().Trim())
						{
							case "peremployeejobmapid":	
								break;
						
							default:
								salaryGridDataView.Table.Columns.RemoveAt(columnCount);
								break;
						}
					}
				}
				DataTable SalaryGridDetails = salaryGridDataView.Table.Clone();
				SalaryGridDetails.TableName = "SalaryGrid";
				foreach (DataRowView drvw in salaryGridDataView)
					SalaryGridDetails.ImportRow(drvw.Row);
				
				// copy the the table containing the Column Totals.
				DataTable ColumnTotals = salaryGridData.Tables[1].Copy(); 
				ColumnTotals.TableName = "Totals";
				// copy the table containing the row count and feed date.
				DataTable TotalRowCount = salaryGridData.Tables[2].Copy();
				TotalRowCount.TableName = "RowCount";

				//Create the dataset with 3 tables
				DataSet resultset = new DataSet();

				resultset.Tables.Add(SalaryGridDetails);
				resultset.Tables.Add(ColumnTotals);
				resultset.Tables.Add(TotalRowCount);

				DataContainer = resultset;
			}
			catch (Exception ex)
			{
				DataContainer = null;
				throw(new Exception(String.Format("{0} {1}",Utility.ExceptionReport.GetExceptionInfo(ex, false),"This occurred while getting Salary Grid details."),ex.InnerException));
			}
		}
		#endregion
	}
}
